package cn.jetpiece.cloud.resource.app.modular.scan.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.jetpiece.cloud.resource.api.ResourceApi;
import cn.jetpiece.cloud.resource.api.mode.query.ResourceQuery;
import cn.jetpiece.cloud.resource.api.mode.result.ResourceResult;
import cn.jetpiece.cloud.model.page.PageResult;
import cn.jetpiece.cloud.model.response.Result;
import cn.jetpiece.cloud.resource.app.modular.scan.entity.Resource;
import cn.jetpiece.cloud.resource.app.modular.scan.service.ResourceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * 资源表控制器
 *
 * @author XuJZ
 * @since 2020-01-09 09:15:44
 */
@RestController
public class ResourceController implements ResourceApi {

    @Autowired
    private ResourceService resourceService;

    @Override
    public Result<PageResult<ResourceResult>> page(ResourceQuery query) {
        return Result.OK(resourceService.page(query));
    }

    @Override
    public ResourceResult getByUrl(String url, String httpMethod) {
        LambdaQueryWrapper<Resource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Resource::getUrl,url);
        wrapper.eq(Resource ::getHttpMethod,httpMethod);
        Resource one = resourceService.getOne(wrapper);
        ResourceResult resourceResult = BeanUtil.toBean(one, ResourceResult.class);
        return resourceResult;
    }

}
